<?php

namespace App\Models;

use App\Models\Traits\HasTreeStructure;
use App\Models\Traits\HasStatus;
use App\Models\Traits\HasMenuType;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Menu extends Model
{
    use HasFactory, SoftDeletes, HasTreeStructure, HasStatus, HasMenuType;

    protected $fillable = [
        'parent_id',
        'title',
        'name',
        'path',
        'component',
        'icon',
        'type',
        'permission',
        'status',
        'sort',
        'hidden',
        'keep_alive',
        'remark',
    ];

    protected $casts = [
        'parent_id' => 'integer',
        'type' => 'integer',
        'status' => 'integer',
        'sort' => 'integer',
        'hidden' => 'integer',
        'keep_alive' => 'integer',
    ];

    public function parent()
    {
        return $this->belongsTo(Menu::class, 'parent_id');
    }

    public function children()
    {
        return $this->hasMany(Menu::class, 'parent_id')->orderBy('sort');
    }

    public function roles()
    {
        return $this->belongsToMany(Role::class, 'role_menus');
    }
}

